package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    //将mapper层进行注入
    @Autowired
    private CategoryMapper categoryMapper;

    //通过类型查询分类的功能
    @Override
    public List<Category> selectCategoryByType(Integer type) {
        //通过传入的type数据 传入到mapper层的pageSelectCategory方法中

        //使用的也是实体类 这里不想接收了 直接在形参里面构造实体类对象并且将集合返回
        return categoryMapper.pageSelectCategory(Category.builder().type(type).build());
    }

    //分页查询分类的功能
    @Override
    public PageResult pageSelectCategory(CategoryPageQueryDTO categoryPageQueryDTO) {

        //一样的步骤 创建实体类对象 拷贝传入的DTO对象
        Category category = new Category();
        BeanUtils.copyProperties(categoryPageQueryDTO, category);

        //通过pageselectCategory的方法传入该对象 获得的集合直接进行转型接受
        Page<Category> p = categoryMapper.pageSelectCategory(category);

        //创建PageResult对象 通过构造器生成对象并且返回
        return new PageResult(p.getTotal(), p.getResult());
    }

    //修改分类的功能
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        //也是将DTO进行拷贝并传入修改
        Category category = Category.builder()
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.updateCategory(category);
    }

    //分类启用/暂停的功能
    @Override
    public void statusStartOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                //.updateTime(LocalDateTime.now())
                //.updateUser(BaseContext.getCurrentId())
                .build();

        categoryMapper.updateCategory(category);
    }

    //添加分类的功能
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                //.createTime(LocalDateTime.now())
                //.createUser(BaseContext.getCurrentId())
                //.updateUser(BaseContext.getCurrentId())
                //.updateTime(LocalDateTime.now())
                .status(StatusConstant.DISABLE)
                .build();
        BeanUtils.copyProperties(categoryDTO, category);
        categoryMapper.addCategory(category);
    }

    //删除分类的功能
    @Override
    public void deleteCategory(Long id) {
        //统计分类有无关联菜品
        Integer count = categoryMapper.selectByCategoryIdDish(id);

        //如果返回的不等于0 说明有关联的菜品 抛出异常
        if (count != 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //如果无关联的菜品 接着来判断有无关联的套餐 如果有 也抛异常
        Integer count2 = categoryMapper.selectByCategoryIdSetmeal(id);
        if (count2 != 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //如果无关联的菜品以及套餐 根据id获得分类
        Category category = categoryMapper.selectCategoryById(id);

        //如果分类是被起售的状态 也需要抛出异常
        if (category.getStatus() == StatusConstant.ENABLE) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_ON_ENABLE);
        }

        //如果是停售则通过删除的方法 传入id删除该分类
        categoryMapper.deleteCategory(id);
    }
}
